home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / ada / adaed-1.11 / adaed-1 / Adaed-1.11.0a / libf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-07  |  6.9 KB  |  325 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9.  
  10. /* libf - auxiliary procedures for reading in IFILE format files.
  11.  * This is subset of procedures needed to read files generated in
  12.  * library format without the need all the library primitives.
  13.  */
  14.  
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include "config.h"
  19. #include "ifile.h"
  20. #include "miscprots.h"
  21. #include "libfprots.h"
  22. #ifndef TRUE
  23. #define TRUE 1
  24. #endif
  25.  
  26. #ifdef DEBUG
  27. #define IOT
  28. #endif
  29.  
  30. /* iot_opt_number is set to give file numbers for traces*/
  31. static int iot_opt_number = TRUE;
  32. static int iot_opt_desc = TRUE;
  33. static int iot_off_trace = TRUE; /* set to trace file positions */
  34.  
  35. void iot_off_info(int lev)                                    /*;iot_off_info*/
  36. {
  37.     iot_off_trace = lev;
  38. }
  39.  
  40. int getint(IFILE *ifile, char *desc)                            /*;getint*/
  41. {
  42.     /* read int  from input file */
  43.     int si;
  44.     int n = 0;
  45.     int    nr;
  46.  
  47. #ifdef IOT
  48.     if (ifile->fh_trace == 2) iot_info(ifile, desc);
  49. #endif
  50. #ifdef HI_LEVEL_IO
  51.     nr = fread((char *) &si, sizeof(int), 1, ifile->fh_file);
  52.     if (nr != 1) {
  53. #else
  54.     nr = read(ifile->fh_file, (char *) &si, sizeof(int));
  55.     if (nr != sizeof(int)) {
  56. #endif
  57.         chaos("libr.c: read_ais - unable to read int value");
  58.     }
  59.     n = si;
  60. #ifdef IOT
  61.     if (ifile->fh_trace == 2) printf(" (int) %ld\n", n);
  62. #endif
  63.     return n;
  64. }
  65.  
  66. int getnum(IFILE *ifile, char *desc)                                /*;getnum*/
  67. {
  68.     /* read integer (only 2 bytes) from input file */
  69.  
  70.     short si;
  71.     int n = 0;
  72.  
  73. #ifdef IOT
  74.     if (ifile->fh_trace == 2) iot_info(ifile, desc);
  75. #endif
  76. #ifdef HI_LEVEL_IO
  77.     fread((char *) &si, sizeof(short), 1, ifile->fh_file);
  78. #else
  79.     read(ifile->fh_file, (char *) &si, sizeof(short));
  80. #endif
  81.     n = si;
  82. #ifdef IOT
  83.     if (ifile->fh_trace == 2) printf(" %d\n", n);
  84. #endif
  85.     return n;
  86. }
  87.  
  88. int getchr(IFILE *ifile, char *desc)                                /*;getchr*/
  89. {
  90.     /* This is variant of getnum used when reading character values */
  91.     /* read integer (only 2 bytes) from input file */
  92.  
  93.     short si;
  94.     int n = 0;
  95.  
  96. #ifdef IOT
  97.     if (ifile->fh_trace == 2) iot_info(ifile, desc);
  98. #endif
  99. #ifdef HI_LEVEL_IO
  100.     fread((char *) &si, sizeof(short), 1, ifile->fh_file);
  101. #else
  102.     read(ifile->fh_file, (char *) &si, sizeof(short));
  103. #endif
  104.     n = si;
  105. #ifdef IOT
  106.     if (ifile->fh_trace == 2) printf(" %d %c\n", n, (char)n);
  107. #endif
  108.     return n;
  109. }
  110.  
  111. long getlong(IFILE *ifile, char *desc)                            /*;getlong*/
  112. {
  113.     /* read long  from input file */
  114.  
  115.     long si;
  116.     long n = 0;
  117.     int    nr;
  118.  
  119. #ifdef IOT
  120.     if (ifile->fh_trace == 2) iot_info(ifile, desc);
  121. #endif
  122.  
  123. #ifdef HI_LEVEL_IO
  124.     nr = fread((char *) &si, sizeof(long), 1, ifile->fh_file);
  125.     if (nr != 1) {
  126. #else
  127.     nr = read(ifile->fh_file, (char *) &si, sizeof(long));
  128.     if (nr != sizeof(long)) {
  129. #endif
  130.         chaos("libr.c: read_ais - unable to read long value");
  131.     }
  132.     n = si;
  133. #ifdef IOT
  134.     if (ifile->fh_trace == 2) printf(" (long) %ld\n", n);
  135. #endif
  136.     return n;
  137. }
  138.  
  139. char *getstr(IFILE *ifile, char *desc)                            /*;getstr*/
  140. {
  141.     char    *s, *p;
  142.     int        n, i;
  143.  
  144. #ifdef IOT
  145.     if (ifile->fh_trace == 1) printf("getstr(ifile, )\n");
  146.     if (ifile->fh_trace == 2) iot_info(ifile, "str");
  147. #endif
  148.     n = getnum(ifile, "");
  149.     if (n == 0) return (char *)0;
  150.     s = (char *) smalloc((unsigned) n);
  151.     p = s;
  152.     for (i = 1; i < n; i++) {
  153. #ifdef HI_LEVEL_IO
  154.         *p++ = getc(ifile->fh_file);
  155. #else
  156.         read(ifile->fh_file, p++, 1);
  157. #endif
  158.     }
  159.     *p = '\0'; /* set end of string*/
  160.  
  161. #ifdef IOT
  162.     if (ifile->fh_trace == 2) printf("%s\n", s);
  163. #endif
  164.     return s;
  165. }
  166.  
  167. #ifdef IOT
  168. void iot_info(IFILE *ifile, char *s)                            /*;iot_info*/
  169. {
  170.     long pos;
  171.  
  172.     if (ifile->fh_trace<2) return;
  173.     if (strlen(s) == 0) return; /* skip if null string */
  174.     pos = iftell(ifile);
  175.     if (iot_opt_desc) printf("%s ", s);
  176.     if (iot_opt_number) printf("%d", ifile->fh_number);
  177.     printf("%c", ifile->fh_type);
  178.     if (iot_off_trace) printf("=%ld", pos);
  179.     printf(" ");
  180. }
  181. #endif
  182.  
  183. long read_init(IFILE *ifile)                                /*;read_init*/
  184. {
  185.     /* initialize read, position at start of first record, return
  186.      * offset of next record. return 0 if no first first record.
  187.      * read first word in file that may have offset to slot info.
  188.      */
  189.  
  190.     long  pos, start;
  191.     int   nr;
  192.  
  193.     if (ifile->fh_trace>1) start = iftell(ifile);
  194. #ifdef HI_LEVEL_IO
  195.     nr = fread((char *) &pos, sizeof(long), 1, ifile->fh_file);
  196.     if (nr != 1) {
  197. #else
  198.     nr = read(ifile->fh_file, (char *) &pos, sizeof(long));
  199.     if (nr != sizeof(long)) {
  200. #endif
  201.         /* temporary fix: permit read_init to fail for stub files ds 11-15*/
  202.         if (ifile->fh_type == 's') return 0;
  203.         chaos("read_init read failed ");
  204.         return 0;
  205.     }
  206. #ifdef IOT
  207.     if (ifile->fh_trace>1) printf("read_init at %ld got %ld\n", start, pos);
  208. #endif
  209.     return pos;
  210. }
  211.  
  212. long read_next(IFILE *ifile, long p)                        /*;read_next*/
  213. {
  214.     int     nr;
  215.     long  pos;
  216.  
  217.     ifseek(ifile, "next-unit", p, 0);
  218.     if (p == ifile->fh_units_end) {
  219. #ifdef IOT
  220.         if (ifile->fh_trace) printf("read_next units_end reached %ld\n", p);
  221. #endif
  222.         return 0; /* if at end */
  223.     }
  224. #ifdef HI_LEVEL_IO
  225.     nr = fread((char *) &pos, sizeof(long), 1, ifile->fh_file);
  226.     if (nr != 1) {
  227. #else
  228.     nr = read(ifile->fh_file, (char *) &pos, sizeof(long));
  229.     if (nr != sizeof(long)) {
  230. #endif
  231.         chaos("read_next read failure");
  232.         return 0;
  233.     }
  234.     return pos;
  235. }
  236.  
  237. void iot_set_opt_number(int val)                    /*;iot_set_opt_number*/
  238. {
  239.     /* set status of fnumber flag that when on causes internal file number
  240.      * to be include in trace output
  241.      */
  242.     iot_opt_number = val;
  243. }
  244.  
  245. void iot_set_opt_desc(int val)                        /*;iot_set_opt_desc*/
  246. {
  247.     /* set status of fnumber flag that when on causes internal file number
  248.      * to be include in trace output
  249.      */
  250.     iot_opt_desc = val;
  251. }
  252.  
  253. void putnum(IFILE *ofile, char *desc, int n)                        /*;putnum*/
  254. {
  255.     /* write integer (as a short) to output file */
  256.  
  257.     short s;
  258.     s = n;
  259.  
  260. #ifdef IOT
  261.     if (ofile->fh_trace > 1) {
  262.         iot_info(ofile, desc);
  263.         printf(" %d\n", n);
  264.     }
  265. #endif
  266. #ifdef HI_LEVEL_IO
  267.     fwrite((char *) &s, sizeof(short), 1, ofile->fh_file);
  268. #else
  269.     write(ofile->fh_file, (char *) &s, sizeof(short));
  270. #endif
  271. }
  272.  
  273. void putpos(IFILE *ofile, char *desc, int n)                    /*;putpos*/
  274. {
  275.     /* like putnum, but verifies that argument positive */
  276.     /* write integer (as a short) to output file */
  277.  
  278.     if (n < 0) chaos("putpos: negative argument");
  279.     putnum(ofile, desc, n);
  280. }
  281.  
  282. void putstr(IFILE *ofile, char *desc, char *s)                    /*;putstr*/
  283. {
  284.     if (s == (char *)0) {
  285. #ifdef IOT
  286.         if (ofile->fh_trace>1) iot_info(ofile, desc);
  287. #endif
  288.         putnum(ofile, "", 0);
  289.     }
  290.     else {
  291. #ifdef IOT
  292.         if (ofile->fh_trace>1) iot_info(ofile, desc);
  293. #endif
  294.         putnum(ofile, "", strlen(s)+1);
  295. #ifdef IOT
  296.         if (ofile->fh_trace>1) printf("%s\n", s);
  297. #endif
  298. #ifdef HI_LEVEL_IO
  299.         fputs(s, ofile->fh_file);
  300. #else
  301.         write(ofile->fh_file, s, strlen(s));
  302. #endif
  303.     }
  304. }
  305.  
  306. void putchr(IFILE *ofile, char *desc, int n)                    /*;putchr*/
  307. {
  308.     /* variant of putnum used when writing character value */
  309.     /* write integer (as a short) to output file */
  310.  
  311.     short s = n;
  312.  
  313. #ifdef IOT
  314.     if (ofile->fh_trace>1) {
  315.         iot_info(ofile, desc);
  316.         printf(" %d %c\n", n, n);
  317.     }
  318. #endif
  319. #ifdef HI_LEVEL_IO
  320.     fwrite((char *) &s, sizeof(short), 1, ofile->fh_file);
  321. #else
  322.     write(ofile->fh_file, (char *) &s, sizeof(short));
  323. #endif
  324. }
  325.